package com.dayuanit.dy16.ebook.ebookweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;

@Controller
public class PictureController extends BaseController {

    @RequestMapping("/picture/{pictureName}")
    public void showPicture(@PathVariable("pictureName") String pictureName, HttpServletResponse response) {
        String path = picturePath + pictureName;

        try (FileInputStream fileInputStream = new FileInputStream(path);) {
            ServletOutputStream outputStream = response.getOutputStream();

            byte[] buff = new byte[1024];
            int length = -1;
            while (-1 != (length = fileInputStream.read(buff))) {
                outputStream.write(buff, 0, length);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
